package bbs.controller;


import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import bbs.annotation.GlobalInterceptor;
import bbs.annotation.VerifyParam;
import bbs.controller.base.ABaseController;
import bbs.entity.dto.SessionWebUserDto;
import bbs.entity.enums.*;
import bbs.entity.po.ForumComment;
import bbs.entity.po.LikeRecord;
import bbs.entity.query.ForumCommentQuery;
import bbs.entity.vo.ResponseVO;
import bbs.exception.BusinessException;
import bbs.service.ForumCommentService;
import bbs.service.LikeRecordService;
import bbs.service.UserInfoService;
import bbs.utils.StringTools;
import bbs.utils.SysCacheUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@RestController("forumCommentController")
@RequestMapping({"/comment"})
public class ForumCommentController extends ABaseController {
    @Resource
    private ForumCommentService forumCommentService;

    @Resource
    private LikeRecordService likeRecordService;

    @Resource
    private UserInfoService userInfoService;


    /**
     * 读取评论
     * @param session
     * @param articleId
     * @param pageNo
     * @param orderType
     * @return
     */
    @RequestMapping({"/loadComment"})
    @GlobalInterceptor(checkParams = true)
    public ResponseVO loadComment(HttpSession session,
                                  @VerifyParam(required = true) String articleId,
                                  Integer pageNo,
                                  Integer orderType) {
        if (!SysCacheUtils.getSysSetting().getCommentSetting().getCommentOpen()) throw new BusinessException(ResponseCodeEnum.CODE_600);

        ForumCommentQuery forumCommentQuery = new ForumCommentQuery();
        forumCommentQuery.setArticleId(articleId);
        forumCommentQuery.setLoadChildren(Boolean.valueOf(true));

        String str = (orderType == null || orderType == 0) ? "good_count desc,comment_id asc" : "comment_id desc";

        forumCommentQuery.setOrderBy("top_type desc," + str);
        forumCommentQuery.setPageNo(pageNo);

        SessionWebUserDto userDto = getUserInfoFromSession(session);
        if (userDto != null) {
            forumCommentQuery.setQueryLikeType(Boolean.valueOf(true));
            forumCommentQuery.setCurrentUserId(userDto.getUserId());
        } else {
            forumCommentQuery.setStatus(ArticleStatusEnum.AUDIT.getStatus());
        }

        forumCommentQuery.setPageSize(Integer.valueOf(PageSize.SIZE50.getSize()));
        forumCommentQuery.setPageNo(pageNo);
        forumCommentQuery.setpCommentId(Integer.valueOf(0));
        forumCommentQuery.setLoadChildren(Boolean.TRUE);
        return getSuccessResponseVO(this.forumCommentService.findListByPage(forumCommentQuery));

    }

    @RequestMapping({"/postComment"})
    @GlobalInterceptor(checkLogin = true, checkParams = true, frequencyType = UserOperFrequencyTypeEnum.POST_COMMENT)
    public ResponseVO postComment(HttpSession session, @VerifyParam(required = true) String articleId, @VerifyParam(required = true) Integer pCommentId, @VerifyParam(min = 2, max = 800) String content, String replyUserId, MultipartFile image)
    {
        if (!SysCacheUtils.getSysSetting().getCommentSetting().getCommentOpen())
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        if (image == null && StringTools.isEmpty(content))
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        SessionWebUserDto userDto = getUserInfoFromSession(session);

//        取用户昵称
        String nickName = userInfoService.getUserInfoByUserId(userDto.getUserId()).getNickName();

        content = StringTools.eecpapeHtml(content);
        ForumComment forumComment = new ForumComment();
        forumComment.setUserId(userDto.getUserId());
        forumComment.setNickName(nickName);
        forumComment.setUserIpAddress(userDto.getProvince());
        forumComment.setpCommentId(pCommentId);
        forumComment.setArticleId(articleId);
        forumComment.setContent(content);
        forumComment.setReplyUserId(replyUserId);
        forumComment.setTopType(CommentTopTypeEnum.NO_TOP.getType());
        forumComment.setPostTime(new Date());
        this.forumCommentService.postComment(forumComment, image);

        if (pCommentId.intValue() != 0) {
            ForumCommentQuery forumCommentQuery = new ForumCommentQuery();
            forumCommentQuery.setArticleId(articleId);
            forumCommentQuery.setpCommentId(pCommentId);
            forumCommentQuery.setOrderBy("top_type desc,comment_id asc");
            forumCommentQuery.setCurrentUserId(userDto.getUserId());
            List list = this.forumCommentService.findListByParam(forumCommentQuery);
            return getSuccessResponseVO(list);
        }
        return getSuccessResponseVO(forumComment);
    }

    @RequestMapping({"/doLike"})
    @GlobalInterceptor(checkLogin = true, checkParams = true, frequencyType = UserOperFrequencyTypeEnum.DO_LIKE)
    public ResponseVO doLike(HttpSession session, @VerifyParam(required = true) Integer commentId)
    {
        SessionWebUserDto userDto = getUserInfoFromSession(session);

        String objectId = String.valueOf(commentId);
        this.likeRecordService.doLike(objectId, userDto.getUserId(), userDto.getNickName(), OperRecordOpTypeEnum.COMMENT_LIKE);
        LikeRecord likeRecord = this.likeRecordService.getLikeRecordByObjectIdAndUserIdAndOpType(objectId, userDto.getUserId(), OperRecordOpTypeEnum.COMMENT_LIKE.getType());
        ForumComment forumComment = this.forumCommentService.getForumCommentByCommentId(commentId);
        forumComment.setLikeType((likeRecord == null) ? null : Integer.valueOf(1));

        return getSuccessResponseVO(forumComment);
    }

    @RequestMapping({"/changeTopType"})
    @GlobalInterceptor(checkLogin = true, checkParams = true)
    public ResponseVO changeTopType(HttpSession session, @VerifyParam(required = true) Integer commentId, @VerifyParam(required = true) Integer topType)
    {
        this.forumCommentService.changeTopType(getUserInfoFromSession(session).getUserId(), commentId, topType);
        return getSuccessResponseVO(null);
    }
}
